<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

/**
 * Class FilesController
 * @package App\Http\Controllers\OA
 * @desc 文件操作类
 */
class FilesController extends Controller
{
    protected $img_ext = array('png', 'jpeg', 'gif', 'psd');

    public function uploadFiles(Request $request)
    {

        try {
            $rules = [
                'dir' => 'required|string',
                'name' => 'sometimes|string'
            ];

            $this->verify($request->all(), $rules);

            $wenjian = $request->file('file');

            $dir = $request->input('dir', 'project');

            if ($wenjian->getError()) throw new \Exception('文件上传失败,稍后再试');
            $disk_name = 'upload_file';
            //获取文件的原文件名 包括扩展名
            $yuanname = $wenjian->getClientOriginalName();

            //获取文件的扩展名
            $kuoname = $wenjian->getClientOriginalExtension();

            //获取文件的类型
            //$type = $wenjian->getClientMimeType();

            //获取文件的绝对路径，但是获取到的在本地不能打开
            $path = $wenjian->getRealPath();

            $size = $wenjian->getSize();

            //要保存的文件名 时间+扩展名
            $filename = $dir . '/' . date('Ymd') . '/' . uniqid() . '.' . $kuoname;
            //保存文件   配置文件存放文件的名字 ，文件名，路径

            //dd($path);

            $context = stream_context_create(array('http' => array('ignore_errors' => true)));

            $contents = file_get_contents($path, FALSE, $context);

            //dd($contents);

            $bool = Storage::disk('public')->put($filename, file_get_contents($path));

            if (!$bool) ret_json('err', 'err', '文件上传失败');

            $fileUrl = env('APP_URL') . '/storage/' . $filename;

            $response = [
                'fileUrl' => $fileUrl,
                'ext' => $kuoname,
                'size' => getSize($size),
                'fileName' => $yuanname,
                'name' => $request->input('name') ?? '',
            ];

            ret_json('ok', '文件上传成功', $response);
        } catch (\Exception $e) {
            ret_json('err', $e->getMessage());
        }
    }

    public function downloadFiles(Request $request)
    {
        $file_path = $request->input('fileUrl');
        $filename = $request->input('fileName');
        $content = file_get_contents($file_path);
        //dd($temp_path);
        Storage::disk('public')->put($filename,$content);
        $temp_path = storage_path('app/public/').$filename;
        $filesize = filesize($temp_path);
        ob_clean();
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Headers: *');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, OPTIONS');
        header('Pragma: public');
        header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control:no-store, no-cache, must-revalidate');
        header('Cache-Control:pre-check=0, post-check=0, max-age=0');
        header('Content-Transfer-Encoding:binary');
        header('Content-Encoding:none');
        //流的方式发送给浏览器
        header("Content-Type: application/octet-stream");
        //按照字节的返回给浏览器
        header("Accept-Ranges: bytes");
        //告诉浏览器文件的大小
        header("Accept-Length: " . $filesize);
        //以附件的形式发送给浏览器(也就是弹出，下载的对话框)
        header("Content-Disposition: attachment; filename=" . $filename);
        //打开文件获取文件句柄
        $handle = fopen($temp_path, "r");
        //将文件直接读取完
        // 一部分一部分的读取
        while (!feof($handle)) {
            $content = fread($handle, 1024);
            echo $content;
        }
        fclose($handle);
        @unlink($temp_path);
        @flush();
        @ob_flush();

    }
}
